package com.hulk.dryad.activiti7.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hulk.dryad.activiti7.api.vo.ActWorkflowFormDataVO;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.persistence.entity.ActTask;
import com.hulk.dryad.persistence.entity.ActWorkflowFormDataModel;
import com.hulk.dryad.persistence.service.IActTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.activiti.api.task.model.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 任务处理
 *
 * @author hulk
 * @date 2019-10-18 17:18:42
 */
@Api(value = "task", tags = "任务处理")
@AllArgsConstructor
@RestController
@RequestMapping("/workflow/task")
public class TaskController {

	private IActTaskService actTaskService;


	@ApiOperation(value = "getTask", notes = "通过tastID获取我的代办任务")
	@PrintlnLog
	@GetMapping(value = "/{taskId}")
	public R<ActTask> geTask(@PathVariable("taskId") String taskId) {
		ActTask actTask =	actTaskService.geTask(taskId);
		return R.ok(actTask);
	}

	@ApiOperation(value = "getPageTask", notes = "获取我的代办任务分页")
	@PrintlnLog
	@GetMapping(value = "/page")
	public R<IPage> getPageTask(Page page) {
		IPage<ActTask> hashMaps = actTaskService.getPageTask(page);
		return R.ok(hashMaps);
	}


	@ApiOperation(value = "formDataShow", notes = "渲染表单")
	@PrintlnLog
	@GetMapping(value = "/formDataShow/{taskId}")
	public R formDataShow(@PathVariable("taskId") String taskId) {
		return R.ok(actTaskService.formDataShow(taskId));
	}


	@ApiOperation(value = "formDataSave", notes = "保存表单")
	@PrintlnLog
	@Idempotent
	@PostMapping(value = "/formDataSave/{taskId}")
	public R formDataSave(@PathVariable("taskId") String taskId,
						  @RequestBody List<ActWorkflowFormDataVO> formData)  {
		List<ActWorkflowFormDataModel> form = formData.stream().map(v -> {
			ActWorkflowFormDataModel model = new ActWorkflowFormDataModel();
			BeanUtils.copyProperties(v, model);
			return model;
		}).collect(Collectors.toList());
		int rows = actTaskService.formDataSave(taskId, form);
		return rows > 0 ? R.ok() : R.failed();

	}

	@ApiOperation(value = "completeTask", notes = "通过tastID完成我的代办任务")
	@PrintlnLog
	@Idempotent
	@PostMapping (value = "/completeTask")
	public R completeTask(@RequestParam("taskId") String taskId ,@RequestBody Map<String, Object> variables) {
		Task tk = actTaskService.completeTask(taskId,variables);
		return tk!= null ? R.ok():R.failed();

	}


    //TODO 拾取任务， 归还任务，委派任务，完成任务。

	/*@ApiOperation(value = "claimTask", notes = "通过tastID拾取任务")
	@PrintlnLog
	@RepeatSubmit
	@GetMapping(value = "/claim/{taskId}")
	public R claimTask(@PathVariable("taskId") String taskId) {
		Task task = taskRuntime.task(taskId);

		if (task.getAssignee() == null) {
			taskRuntime.claim(TaskPayloadBuilder.claim().withtaskId(task.getId()).build());
		}

		return R.ok();
	}
*/


}
